package com.XZDP.BloomFilter;

import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class BloomFilterService {

    private final RedissonClient redissonClient;

    @Autowired
    public BloomFilterService(RedissonClient redissonClient) {
        this.redissonClient = redissonClient;
    }

    //创建布隆过滤器
    public RBloomFilter<String> createBloomFilter(String name, long expectedInsertions, double probabilityOfFalsePositives) {
        RBloomFilter<String> bloomFilter = redissonClient.getBloomFilter(name);
        bloomFilter.tryInit(expectedInsertions, probabilityOfFalsePositives);
        return bloomFilter;
    }

    //添加值
    public void addElement(RBloomFilter<String> bloomFilter, String element) {
        bloomFilter.add(element);
    }

    //检查值
    public boolean containsElement(RBloomFilter<String> bloomFilter, String element) {
        return bloomFilter.contains(element);
    }
}
